#!/bin/sh
#
# CkTestDB -- see if this dialect is has been tested
#
# This script builds a line from config.flags in the form of lines in
# ./TestDB,  (See Add2TestDB.)
#
# It then compares the line to TestDB.  If the line is found, the script
# exits.  if the line is not found, the script issues a warning and requests
# a go-ahead confirmation.
#
# The script will exit 0 if the test line is in the DB or the go-ahead
# confirmation is positive.
#
# $Id: CkTestDB,v 1.3 2010/01/18 19:02:21 abe Exp abe $

# Check for config.flags.

if test ! -r config.cflags
then
  echo "$0: no ./config.cflags file"
  exit 1
fi

# Check for a current data base file.

if test ! -r TestDB
then
  echo "$0: no ./TestDB file"
  exit 1
fi

# Form a data base line.

new=""
for i in $(LC_ALL=C sort < config.cflags)
do
  w=$(echo $i | sed 's/^-D//')
  if test "X$new" = "X"
  then
    new=$w
  else
    new="$new $w"
  fi
done

# See if the line is already in the data base.  Exit with success (0), if it is.

grep "^$new\$" TestDB > /dev/null 2>&1
if test $? -eq 0
then
  exit 0
fi

# This dialect may never have been validated with the test suite.

# If the standard input is not a TTY, quit, because no interaction
# is possible.

tty -s > /dev/null 2>&1
if test $? -ne 0
then
  echo ""
  echo "This suite has not been validated on:"
  echo ""
  echo "     $new"
  echo ""
  exit 1
fi

# Establish trap and stty handling.

ISIG=":"
trap '$ISIG; exit 1'  1 2 3 15
stty -a 2>&1 | grep isig > /dev/null
if test $? -eq 0
then
  stty -a 2>&1 | egrep -e -isig > /dev/null
  if test $? -eq 0
  then
    ISIG="stty -isig"
    stty isig
  fi
fi

# Establish echo type -- Berkeley or SYSV.

j=$(echo -n "")
if test "X$j" = "X-n "
then
  EC="\c"
  EO=""
else
  EC=""
  EO="-n"
fi

if [ x"$CI" = x1 ]; then
    exit 0
fi

# Display a validation warning.

cat << .CAT_MARK > /dev/tty

==================================================================

!!!WARNING!!!

This dialect or its particular version may not have been validated
with the lsof test suite.  Consequently some tests may fail or may
not even compile.

This is the computed identity of this dialect, not found in the
test data base file, ./TestDB:

.CAT_MARK
echo "    $new" > /dev/tty
END=0
while test $END = 0
do
  echo "" > /dev/tty
  echo $EO "Do you want to continue (y|n) [n]? $EC" > /dev/tty
  read ANS EXCESS
  if test "X$ANS" = "Xn" -o "X$ANS" = "XN"
  then
    exit 1
  fi
  if test "X$ANS" = "Xy" -o "X$ANS" = "XY"
  then
    exit 0
  else
    echo "Please answer y or n." > /dev/tty
  fi
done

# Should never get here!

echo "$0: unexpected failure!"
exit 2
